package librus.control
{
	import flash.desktop.ClipboardFormats;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	
	import librus.event.DeleteEvent;
	import librus.model.Card;
	import librus.model.Model;
	import librus.pdf.PdfManager;
	import librus.view.View;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	import mx.managers.DragManager;

	public class Control
	{
		public function Control()
		{
			__model = new Model;
			__progress = new ProgressManager;
		}
		
		private var __view:View;
		private var __model:Model;
		private var __manager:PdfManager;
		
		private var __dragManager:DragDropManager;
		private var __progress:ProgressManager;
		

		public function set view(value:View):void
		{
			__view = value;
		
			__view.addEventListener(FlexEvent.CREATION_COMPLETE, onViewComplete);
		}
		
		protected function onViewComplete(event:FlexEvent):void
		{
			__view.removeEventListener(FlexEvent.CREATION_COMPLETE, onViewComplete);
			
			__view.grid.addEventListener(DeleteEvent.DELETE, onDelete);
			__view.grid.addEventListener(ListEvent.CHANGE, onChange);
			
			__dragManager = new DragDropManager(__view);
			__dragManager.addEventListener(DragDropManager.FILES_ADDED, onFilesAdded);
			
			__view.create_btn.addEventListener(MouseEvent.CLICK, onCreate);
		}
		
		protected function onFilesAdded(event:Event):void
		{
			__model.addCards(__dragManager.cards);
			__view.grid.dataProvider = __model.data;			
		}
		
		protected function onCreate(event:MouseEvent):void
		{
			__progress.show(true,__view);
			
			__manager = new PdfManager(__progress);
			__manager.addEventListener(PdfManager.FILL_COMPLETE, onFillComplete);
			
			__manager.fill(__model.data);			
		}
		
		protected function onFillComplete(event:Event):void
		{
			var file:File = File.desktopDirectory.resolvePath("testPage.pdf");
			
			__manager.removeEventListener(PdfManager.FILL_COMPLETE, onFillComplete);
			__manager.saveAs(file);			
			
			__manager = null;
			
			__progress.show(false);
		}
		
		protected function onChange(event:ListEvent):void
		{
			while (preview.numChildren)
				preview.removeChildAt(0);
			
			var card:Card = __view.grid.selectedItem as Card;
			if (card)
			{
				if (card.data)
				{
					showCard(card);
				}
				else
				{
					card.addEventListener(Card.LOADED, onCardComplete);
					card.load();
				}
			}
		}
		
		protected function onCardComplete(event:Event):void
		{
			var card:Card = event.target as Card;
				card.removeEventListener(Card.LOADED, onCardComplete);
				
			showCard(card);
		}		
		
		private function showCard(card:Card):void
		{
			var bitmap:Bitmap = new Bitmap(card.data);
			
			preview.addChild(bitmap);
			__view.invalidateDisplayList();
		}
		
	
		public function deleteItem(card:Card):void
		{
			var arr:ArrayCollection = __view.grid.dataProvider as ArrayCollection;
			arr.removeItemAt(arr.getItemIndex(card));
		}
		
		protected function onDelete(event:DeleteEvent):void
		{
			event.preventDefault();
			event.stopImmediatePropagation();
			
			deleteItem(event.card);
		}
		
		private function get preview():UIComponent
		{
			return __view.preview;
		}
		
		private function get grid():DataGrid
		{
			return __view.grid;
		}
	}
}